package cn.bjxy.shishaolong.system.service;

import cn.bjxy.shishaolong.system.entity.Resources;
import cn.bjxy.shishaolong.system.entity.Role;
import cn.bjxy.shishaolong.system.entity.RoleResources;
import cn.bjxy.shishaolong.system.exception.OperatorFailureException;
import cn.bjxy.shishaolong.system.mapper.ResourcesMapper;
import cn.bjxy.shishaolong.system.mapper.RoleMapper;
import cn.bjxy.shishaolong.system.mapper.RoleResourcesMapper;
import cn.bjxy.shishaolong.system.service.IRoleService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 时少龙
 * @since 2019-03-31
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RoleResourcesMapper roleResourcesMapper;
    @Autowired
    private ResourcesMapper resourcesMapper;

    @Override
    public boolean judgeRole(Integer roleId, String... roleKeys) {
        Role role = roleMapper.selectById(roleId);
        if (role == null) {
            throw new OperatorFailureException(-1, "角色不存在");
        } else {
            for (String roleKey : roleKeys) {
                if(roleKey.equals(role.getKey())) return true;
            }
            return false;
        }
    }

    @Override
    public Integer getRoleIdByKey(String roleKey) {
        Role role = roleMapper.selectOne(new QueryWrapper<Role>().lambda().eq(Role::getKey, roleKey));
        if(role == null) throw  new OperatorFailureException(-1,"角色不存在");
        return role.getTid();
    }

    @Override
    @Transactional
    public void resourceAuth(Map<String, Object> delMap, List<Integer> resourceIds, Integer roleId) {

        roleResourcesMapper.deleteByMap(delMap);
        RoleResources roleResources=null;
        List<RoleResources> roleResourcesList=new ArrayList<>();
        for(Integer resourceId:resourceIds){
            roleResources=new RoleResources();
            roleResources.setRoleId(roleId);
            roleResources.setResourceId(resourceId);
            roleResourcesList.add(roleResources);
            roleResourcesMapper.insert(roleResources);
        }
        for (Integer resourceId : resourceIds) {
            getParent(resourceId,roleId);
        }
    }

    private void getParent(Integer resourceId,Integer roleId){
        try {
            Resources resource = resourcesMapper.selectById(resourceId);
            Map map = new HashMap();
            map.put("tid", resource.getPid());
            List<Resources> list = resourcesMapper.selectByMap(map);
            if (list != null && list.size() > 0) {
                RoleResources sysRoleResources = new RoleResources();
                sysRoleResources.setResourceId(list.get(0).getTid()).setRoleId(roleId);
                Map temp = new HashMap();
                temp.put("resource_id", list.get(0).getTid());
                temp.put("role_id", roleId);
                List tempList = roleResourcesMapper.selectByMap(temp);
                if (tempList == null || tempList.size() == 0) {
                    roleResourcesMapper.insert(sysRoleResources);
                }
                getParent(list.get(0).getTid(), roleId);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
